﻿component
{
	include "../../inc/lib.cfm";
	//列表操作
	public any function list(any rc)
	{
		var result = {};
		//从对象中进行Hql模式查询
		var offset = (rc.page-1)*rc.rows;
		hql = "from store where " & rc.sqlWhere & rc.sqlSort;

		//writeLog(file='fw1', text="#hql#");

		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from store where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);

		return result;
	}

	//删除操作
	public any function delete(numeric id){
		transaction {
            var store = entityLoadByPk("store", arguments.id);
           	entityDelete(store);
            transactionCommit();
        }
	}

	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("store", arguments.id);
		return result;
	}


	public any function getall(){
		hql = "from store order by id asc";
		var result = ormExecuteQuery(hql, false);
		return result;
	}


	//新增操作
	public any function save(any rc){
		transaction {
            var q = entityNew("store");
			include "../../inc/saveandupdatenotcommit.cfm";
			var targetUrl ='http://api.map.baidu.com/geocoder/v2/?address=' & rc.address & '&output=json&ak=' & request.baiduAk;
			http method="GET" url="#targetUrl#" result="result";
			var geo = DeserializeJSON(result.filecontent);
			var lng = geo.result.location.lng;
			var lat = geo.result.location.lat;
			q.setlongitude(lng);
			q.setlatitude(lat);
			entitySave(q);
			transactionCommit();
        }
        return q;
	}

	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("store", rc.id);

			if(structKeyExists(rc, "address") && len(trim(rc.address))>0)
			{
				 var targetUrl ='http://api.map.baidu.com/geocoder/v2/?address=' & rc.address & '&output=json&ak=' & request.baiduAk;
				http method="GET" url="#targetUrl#" result="result";
				var geo = DeserializeJSON(result.filecontent);
				var lng = geo.result.location.lng;
				var lat = geo.result.location.lat;
				q.setlongitude(lng);
				q.setlatitude(lat);
			}

			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}

	public any function importFile(any rc){
		transaction {
            var q = entityLoadByPk("uploader", rc.id);
			var f = expandpath('uploads/' & q.getservername());
			objPOI = new cfc.POIUtility();
			arrSheets = objPOI.ReadExcel( FilePath = f,HasHeaderRow = true);

			var fields = ['门店名称','门店地址','联系电话','联系人','门店介绍'];

			var column = arrSheets[1].COLUMNNAMES;
			var pos = [];
			for(var i=1; i<= arraylen(column); i++){
				pos.append(arrayfindnocase(fields,column[i]));
			}

			var qry = arrSheets[1].Query;

			for(var i=1; i<= qry.RecordCount; i++){
				var name = evaluate("qry.column"  & arrayfind(pos,1) & '[' & i& ']');
				var address = evaluate("qry.column"  & arrayfind(pos,2) & '[' & i& ']');
				var phone = evaluate("qry.column"  & arrayfind(pos,3) & '[' & i& ']');
				var linkman = evaluate("qry.column"  & arrayfind(pos,4) & '[' & i& ']');
				var content = evaluate("qry.column"  & arrayfind(pos,5) & '[' & i& ']');

				var hql = "from store where name = '#name#' and address = '#address#' and phone='#phone#' and linkman='#linkman#' and content = '#content#'";
				var result = ormExecuteQuery(hql,true);

				if(isnull(result)){
					var q = entityNew("store");
					q.setname(name);
					q.setaddress(address);
					q.setlinkman(linkman);
					q.setphone(phone);
					q.setcontent(content);

					var targetUrl ='http://api.map.baidu.com/geocoder/v2/?address=' & address & '&output=json&ak=' & request.baiduAk;
					http method="GET" url="#targetUrl#" result="result";
					var geo = DeserializeJSON(result.filecontent);
					var lng = geo.result.location.lng;
					var lat = geo.result.location.lat;
					q.setlongitude(lng);
					q.setlatitude(lat);

					entitySave(q);
				}
			}

			transactionCommit();
        }
        return q;
	}


	public any function exportFile(){
		objPOI = new cfc.POIUtility();
	  	qry = new Query(); // new query object
	    qry.setSQL("select name,address,linkman,phone,longitude,latitude, CONVERT(content, CHAR(5000)) as content  from store"); //set query
	    qryRes = qry.execute(); // execute query

		objSheet = objPOI.GetNewSheetStruct();
		objSheet.Query = qryRes.getResult();
		objSheet.ColumnList = "name,address,linkman,phone,longitude,latitude,content";
		objSheet.ColumnNames = "门店名称,联系地址,联系人员,联系电话,经度,纬度,门店介绍";
		objSheet.SheetName = "Store";

		objPOI.WriteExcel(FilePath = ExpandPath( "./store.xls" ),Sheets = objSheet);

		header  name="content-disposition" value="attachment; filename=store.xls";
		content file="#expandpath('./store.xls')#" deletefile="yes";
	}

}